package 多线程初阶.示例测试;

/**
 * join的作用:等待线程结束
 */
public class TestDemo8 {
    private static class MyRunnable implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()+" is run");
                try {
                    Thread.sleep(1*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+" will die");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable targetA=new MyRunnable();
        Runnable targetB=new MyRunnable();
        Thread threadA=new Thread(targetA,"threadA");
        Thread threadB=new Thread(targetB,"threadB");
        System.out.println(Thread.currentThread().getName()+" is run");
        threadA.start();
        threadA.join();//main等待threadA结束才会往下执行
        threadB.start();
        threadB.join();//main等待threadB结束才会往下执行
        System.out.println(Thread.currentThread().getName()+" will die");
    }
}
